package cn.com.pajx.pajx_visitor_v3.ui.fragment;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import org.greenrobot.eventbus.EventBus;

import java.util.LinkedHashMap;

import butterknife.BindView;
import butterknife.OnClick;
import cn.com.pajx.pajx_visitor_v3.R;
import cn.com.pajx.pajx_visitor_v3.api.Api;
import cn.com.pajx.pajx_visitor_v3.base.BaseMvpFragment;
import cn.com.pajx.pajx_visitor_v3.bean.EventMessage;
import cn.com.pajx.pajx_visitor_v3.mvp.presenter.LoadDataPresenterImpl;
import cn.com.pajx.pajx_visitor_v3.utils.AppConstant;
import cn.com.pajx.pajx_visitor_v3.utils.SharePreferencesUtil;
import cn.com.pajx.pajx_visitor_v3.utils.ToastUtil;

/**
 * 解绑学校的页面
 * create an instance of this fragment.
 */
@SuppressLint("NonConstantResourceId")
public class UnbindSchoolFragment extends BaseMvpFragment<LoadDataPresenterImpl> {

    @BindView(R.id.view_admin)
    EditText viewAdminPassword;
    @BindView(R.id.iv_close)
    ImageView ivClose;
    @BindView(R.id.tv_change_title)
    TextView tvChangeTitle;
    @BindView(R.id.tv_unbind)
    TextView tvUnbind;

    private String admin_password;

    public UnbindSchoolFragment() {
    }

    public static UnbindSchoolFragment newInstance() {
        return new UnbindSchoolFragment();
    }

    @Override
    protected void beforeInitHead() {
        super.beforeInitHead();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_unbind_school;
    }

    @Override
    protected double loadWidthScale() {
        return 0.45;
    }

    @Override
    protected LoadDataPresenterImpl initPresenter() {
        return new LoadDataPresenterImpl();
    }

    @Override
    protected void initView(View view, Bundle savedInstanceState) {
        ivClose.setVisibility(View.VISIBLE);
        tvChangeTitle.setText("解除绑定");
        tvUnbind.setText("确定解除绑定");
    }

    /**
     * 加载拜访的预约信息
     */
    private void unbindSchool() {
        LinkedHashMap<String, String> map = new LinkedHashMap<>();
        map.put("pw_admin", admin_password);
        mPresenter.loadData(Api.UNBIND_SCHOOL, map, "UNBIND_SCHOOL", "正在加载");
    }

    @OnClick({R.id.tv_unbind, R.id.iv_close})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_unbind://解除绑定
                if (checkParams()) return;
                hideSoftInput();
                //绑定数据
                unbindSchool();
                break;
            case R.id.iv_close://关闭
                dismiss();
                break;
        }
    }

    private void showBindSchoolView() {
        //清除全部数据
        SharePreferencesUtil.getInstance().clearAll();
        //关闭页面
        dismiss();
        //关闭设置页面
        EventBus.getDefault().post(new EventMessage(AppConstant.SETTING));
    }

    /**
     * 检验参数
     *
     * @return 布尔值 true有空数据 false 通过
     */
    private boolean checkParams() {
        admin_password = viewAdminPassword.getText().toString().trim();
        if (TextUtils.isEmpty(admin_password)) {
            ToastUtil.toast("请输入管理密码");
            return true;
        }
        return false;
    }

    @Override
    public void onSuccess(String data, String aesKey, String message, int code, String tag) {
        super.onSuccess(data, aesKey, message, code, tag);
        showBindSchoolView();
    }
}